package com.ykt.mall.ctrl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.ykt.mall.content.Content;
import com.ykt.model.ProductType;
import com.ykt.model.Shop;

@ControllerBind(controllerKey="/product")//viewPath="/template"
public class ProductCtrl  extends Controller {
	private static Logger log = Logger.getLogger(ProductCtrl.class);

	public void view() {

		Integer shopId = getParaToInt(0);
		
		if(isParaExists(1)){
			setAttr("dayFlag",getParaToInt(1));
		}

		List<ProductType> list = ProductType.dao.find("select * from product_type where pid>0 and shop_id= ? order by seq ;",shopId);

		Map<Integer,List<Record>> map = new TreeMap<Integer,List<Record>>();
		
		for(ProductType ptype : list){
			int id = ptype.getInt("id");
			List<Record> pList = Db.find(" select id,p.name,now_price,logo,p.desc from product p where product_type_id = ? order by seq ",id);
			map.put(id, pList);
		}
		
		Shop shopPo = Shop.dao.findById(shopId);
		if(getSessionAttr(Content.SESSION_SHOP) == null){
			setSessionAttr(Content.SESSION_SHOP, shopPo);
		}
		
		setAttr("shopPo", shopPo);
		setAttr("productTypeList", list);
		setAttr("map",map);
	}

	public static void main(String args[]){
		Map<Integer,String> mp = new HashMap<Integer, String>();
		mp.put(1,"1");
		mp.put(2,"2");
		Iterator<Integer> it = mp.keySet().iterator();
		while(it.hasNext()){
			int id = it.next();
			System.out.println(mp.get(id));
		}

	}

}
